package com.ti.andon.persistent.dao;

import com.ti.andon.persistent.AdEquip;
import com.ti.andon.workflow.dao.base.BaseHibernateDAO;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 	* A data access object (DAO) providing persistence and search support for AdEquip entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.ti.andon.persistent.AdEquip
  * @author MyEclipse Persistence Tools 
 */

public class AdEquipDAO extends BaseHibernateDAO  {
    private static final Log log = LogFactory.getLog(AdEquipDAO.class);
	//property constants
	public static final String EQUIP_CODE = "equipCode";
	public static final String EQUIP_NAME = "equipName";
	public static final String EQUIP_DESC = "equipDesc";
	public static final String EQUIP_TYPE = "equipType";
	public static final String CHANGE_BY = "changeBy";



    
    public void save(AdEquip transientInstance) {
        log.debug("saving AdEquip instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(AdEquip persistentInstance) {
        log.debug("deleting AdEquip instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public AdEquip findById( java.lang.Long id) {
        log.debug("getting AdEquip instance with id: " + id);
        try {
            AdEquip instance = (AdEquip) getSession()
                    .get("com.ti.andon.persistent.AdEquip", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(AdEquip instance) {
        log.debug("finding AdEquip instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.ti.andon.persistent.AdEquip")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding AdEquip instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from AdEquip as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    public String getEquipName(String equipCode)
    {
    	List list = findByProperty("equipCode", equipCode);
    	if (null != list && list.size()>0)
    	{
    		System.out.println("The name is:" + ((AdEquip)list.get(0)).getEquipName());
    		return ((AdEquip)list.get(0)).getEquipName();
    	}
    	return null;
    }
    
	public List findByEquipCode(Object equipCode
	) {
		return findByProperty(EQUIP_CODE, equipCode
		);
	}
	
	public List findByEquipName(Object equipName
	) {
		return findByProperty(EQUIP_NAME, equipName
		);
	}
	
	public List findByEquipDesc(Object equipDesc
	) {
		return findByProperty(EQUIP_DESC, equipDesc
		);
	}
	
	public List findByEquipType(Object equipType
	) {
		return findByProperty(EQUIP_TYPE, equipType
		);
	}
	
	public List findByChangeBy(Object changeBy
	) {
		return findByProperty(CHANGE_BY, changeBy
		);
	}
	

	public List findAll() {
		log.debug("finding all AdEquip instances");
		try {
			String queryString = "from AdEquip";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public AdEquip merge(AdEquip detachedInstance) {
        log.debug("merging AdEquip instance");
        try {
            AdEquip result = (AdEquip) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(AdEquip instance) {
        log.debug("attaching dirty AdEquip instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(AdEquip instance) {
        log.debug("attaching clean AdEquip instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}